﻿using System;
using System.IO;
using System.Xml.Serialization;
using MobySharp.Results;

namespace MobySharp.Actions
{
    public class BaseAction
    {
        public BaseAction()
        {
            ResponseFormat = FormatType.xml;
        }

        [PostDataInstruction("k", true, 32)]
        public string Key { get; set; }

        [PostDataInstruction("format", true)]
        public FormatType ResponseFormat { get; set; }

        [PostDataInstruction("action", true)]
        public APICall Action { get; set; }

        public HTTPRequestType RequestType { get; set; }

        public IResult ParseResult(string result)
        {
            BasicResult m = null;
            try
            {
                if (result.Contains("<?xml"))
                {
                    var serializer = new XmlSerializer(typeof (BasicResult));
                    m = (BasicResult) serializer.Deserialize(new StringReader(result));

                    return m;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return m;
        }
    }
}